<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="icon" href="/favicon.ico" type="image/ico">
    <title>花云云盘管理页面</title>
    <link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css">
    <!--<link rel="stylesheet" href="/bootstrap/bootstrap-table/bootstrap-table.css">-->

    <!--在线音乐播放功能-->
    <link rel="stylesheet" href="/aplayer/APlayer.min.css">
    <script src="/aplayer/APlayer.min.js"></script>

    <script src="/js/jquery.min.js"></script>
    <!--<script src="/js/jquery.zclip.js"></script>-->
    <script src="/bootstrap/js/bootstrap.min.js"></script>
    <!--<script src="/bootstrap/bootstrap-table/bootstrap-table.js"></script>-->
    <!--<script src="/bootstrap/bootstrap-table/bootstrap-table-zh-CN.js"></script>-->
    <script src="/webuploader/webuploader.js"></script>
    <!--剪切板的功能-->
    <script src="/js/clipboard.min.js"></script>

    <style>
        a {
            font-family: "微软雅黑";
            cursor: pointer;
        }

        a.list-group-item:hover {
            background-color: rgba(51, 150, 200, 0.8);
        }

        tr {
            font-family: "微软雅黑";
        }

        .blur {
            filter: url(blur.svg#blur); /* FireFox, Chrome, Opera */
            -webkit-filter: blur(7px); /* Chrome, Opera */
            -moz-filter: blur(7px);
            -ms-filter: blur(7px);
            filter: blur(7px);
            filter: progid:DXImageTransform.Microsoft.Blur(PixelRadius=7, MakeShadow=false); /* IE6~IE9 */
        }

        .loading {
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0%;
            left: 0%;
            line-height: 56px;
            background: #000;
            color: #fff;
            padding-left: 60px;
            font-size: 15px;
            opacity: 0.7;
            z-index: 9999;
            filter: progid:DXImageTransform.Microsoft.Alpha(opacity=70);
        }

        .input-group[class*="col-"] {
            float: right;
            padding-right: 20px;
        }

        /* 媒体查询：适配移动端(0-480px屏幕)的样式 */
        @media only screen and (max-width: 480px) {
            .input-group {
                margin-top: 7px !important;
            }
        }

        .strogeSpaceBar {
            background: #4cae4c;
            height: 20px;
            text-align: center;
            font-weight: bold;
            font-family: 'Microsoft Yahei';
            color: #FFF;
        }
    </style>

<body>

<div id="imgbackground" class="blur"
     style="background: url('images/bg3.jpg');top:0; position: fixed;width: 100%;height: 100%;z-index: -1;">
</div>
<div id="container" class="container"
     style="width: 90%;margin-top:20px;margin-bottom: 20px; background-color: rgba(255,255,255,0.7);border-radius: 10px;">
    <div class="header clearfix" style="margin: 0px; ">
        <img src="/images/pan-top.png" style="max-height: 55px;">
        <span>
                <ul class="nav nav-pills pull-right" style="margin-top: 10px;">
<!--                    author 是后端model过来的-->
                    <li role="presentation"><a id="userLName" href="/" th:text="${author}"
                                               style="color: firebrick;background: unset;"></a></li>
                    <!--                    跳转到主页-->
                    <li id="homeBtn" role="presentation"><a href="/">主页</a></li>
                    <li id="quitBtn" role="presentation"><a style="cursor: pointer">退出</a></li>
                </ul>
            </span>
    </div>
    <div id="player_music"></div>
    <div class="row" style="margin-top: 15px;">
        <div class="col-sm-12">
            &nbsp;&nbsp;
            <button class="btn btn-default" onclick="refreshUserList();"><i class="glyphicon glyphicon-refresh"></i>
                刷新
            </button>
            &nbsp;&nbsp;
            <button class="btn btn-default" onclick="registerCode()"><i class="glyphicon glyphicon-fire"></i>
                注册码生成
            </button>

            <div class="input-group col-sm-3">
                <input type="text" id="find" autocomplete="off"
                       class="form-control" placeholder="搜索用户"/>
                <span class="input-group-btn">
               <input type="button" id="search" class="btn btn-info btn-search"
                      onclick="findUser()" value="搜索"/>
            </span>
            </div>

        </div>
    </div>
    <br/>
    <!-- /.row -->

    <div id="table_file_bg" class="table-responsive" style="max-height: 500px;overflow: auto;">
        <table id="table_filelist" class="table table-hover">
            <thead>
            <tr>
                <th style="width: 30px;vertical-align: middle;"></th>
                <th>用户名</th>
                <th>邮箱</th>
                <th>联系方式</th>
                <th>注册时间</th>
                <th></th>
                <th>操作</th>
                <th></th>
            </tr>
            </thead>
        </table>
    </div>

    <footer class="footer" style="margin: 0px; margin-top: 40px;">
        <p style="text-align: center; font-family: '微软雅黑'; color: #ff8c00;">
            云花云盘
        </p>
    </footer>
</div>

<script>
    (function($) {
        $.extend({
            myTime: {
                /**
                 * 当前时间戳
                 * @return <int>    unix时间戳(秒)
                 */
                CurTime: function(){
                    return Date.parse(new Date())/1000;
                },
                /**
                 * 日期 转换为 Unix时间戳
                 * @param <string> 2014-01-01 20:20:20 日期格式
                 * @return <int>    unix时间戳(秒)
                 */
                DateToUnix: function(string) {
                    var f = string.split(' ', 2);
                    var d = (f[0] ? f[0] : '').split('-', 3);
                    var t = (f[1] ? f[1] : '').split(':', 3);
                    return (new Date(
                        parseInt(d[0], 10) || null,
                        (parseInt(d[1], 10) || 1) - 1,
                        parseInt(d[2], 10) || null,
                        parseInt(t[0], 10) || null,
                        parseInt(t[1], 10) || null,
                        parseInt(t[2], 10) || null
                    )).getTime() / 1000;
                },
                /**
                 * 时间戳转换日期
                 * @param <int> unixTime  待时间戳(秒)
                 * @param <bool> isFull  返回完整时间(Y-m-d 或者 Y-m-d H:i:s)
                 * @param <int> timeZone  时区
                 */
                UnixToDate: function(unixTime, isFull, timeZone) {
                    if (typeof (timeZone) == 'number')
                    {
                        unixTime = parseInt(unixTime) + parseInt(timeZone) * 60 * 60;
                    }
                    var time = new Date(unixTime);
                    var ymdhis = "";
                    ymdhis += time.getFullYear() + "/";
                    ymdhis += (time.getMonth()+1) + "/";
                    ymdhis += time.getDate();
                    if (isFull === true)
                    {
                        ymdhis += " " + time.getUTCHours() + ":";
                        ymdhis += time.getUTCMinutes() + ":";
                        ymdhis += time.getUTCSeconds();
                    }
                    return ymdhis;
                }
            }
        });
    })(jQuery);

    $fileArray = new Array(), // 要上传的文件列表
        $nameArray = new Array(), // 文件名称
        count = 0;// 正在上传的文件在上传列表中的位置

    var tem;
    var username = $("#userLName").text();
    var create = 0;

    //ie内核判断
    $(function () {
        // 不支持ie浏览器
        if (isIE() == true) {
            alert("网站不支持ie内核的浏览器，请使用Chrome内核的浏览器，现在为您退出！");
            $("#quitBtn").click();
            window.opener = null;
            window.open('', '_self');
            window.close();
        }

        $("#userLName").text("您好，" + $("#userLName").text());
        // 刷新用户文件列表
        refreshUserList();
    });

    //退出登陆
    $('#quitBtn').click(function (e) {
        $.ajax({
            type: "GET",
            url: "api/v1/pan/user/quit",
            dataType: "json",
            success: function (data) {
                if (data.code == "200") {
                    window.location.href = "/old";
                } else {
                    alert("登出失败！");
                }
            }
        });
    });

    // 获取用户列表
    function refreshUserList() {
        var cell_start = 1;
        $.ajax({
            type: "GET",
            url: "/getUserList",
            dataType: "json",
            success: function (data) {
                // 将现在的表内容存储并清空
                var oTabNow = document.getElementById('table_filelist');
                var nowTabRowLen = oTabNow.rows.length;
                for (var i = 1; i < nowTabRowLen; i++) {
                    var rowObj = oTabNow.rows[1];
                    if (isIE()) {
                        rowObj.parentNode.removeChild(rowObj);
                    } else {
                        rowObj.remove();
                    }
                }
                var userIds = new Array();

                for (var i = 0; i < data.total; i++) {
                    var rowObj = oTabNow.insertRow(oTabNow.rows.length); // 添加一行
                    rowObj.insertCell(0).innerHTML = '<img src="images/timg.jpg" style="height: 20px;">';
                    rowObj.insertCell(cell_start).innerHTML = '<a>' + data.records[i].username + '</a>';
                    rowObj.insertCell(cell_start + 1).innerHTML = '<a>' + data.records[i].email + '</a>';
                    rowObj.insertCell(cell_start + 2).innerHTML = '<a>' + data.records[i].phone + '</a>';
                    rowObj.insertCell(cell_start + 3).innerHTML = '<a>' + $.myTime.UnixToDate(data.records[i].createDate,true) + '</a>';
                    rowObj.insertCell(cell_start + 4).innerHTML = '<a>密码重置</a>';
                    rowObj.insertCell(cell_start + 5).innerHTML = '<a>设为管理员</a>';
                    rowObj.insertCell(cell_start + 6).innerHTML = '<a>删除</a>';
                    userIds.push(data.records[i].id);
                    // 密码重置响应事件
                    rowObj.getElementsByTagName('a')[4].onclick = function () {
                        var tempIdx = this.parentNode.parentNode.rowIndex;
                            $.ajax({
                                type: "PUT",
                                url: "/alterPassword",
                                data: {id: userIds[tempIdx-1]},
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == "200") {
                                        alert("密码重置成功，新密码已发送至用户邮箱！")
                                    }else {
                                        alert(data.msg);
                                    }
                                }
                            });
                        }
                        // 权限修改响应事件
                    rowObj.getElementsByTagName('a')[5].onclick = function () {
                        var tempIdx = this.parentNode.parentNode.rowIndex;
                            $.ajax({
                                type: "PUT",
                                url: "/updateAuth",
                                data: {id: userIds[tempIdx-1]},
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == "200") {
                                        refreshUserList();
                                    }else {
                                        alert(data.msg);
                                    }
                                }
                            });
                        }
                    // 删除操作响应事件
                    rowObj.getElementsByTagName('a')[6].onclick = function () {
                        var tempIdx = this.parentNode.parentNode.rowIndex;
                        if(window.confirm("确定删除吗？")){
                                $.ajax({
                                    type: "DELETE",
                                    url: "/deleteUser",
                                    data: {id: userIds[tempIdx-1]},
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == "200"){
                                            refreshUserList();
                                        }else {
                                            alert(data.msg);
                                        }
                                    }
                                });
                            }
                        }
                }
            }
        });
    }

    function registerCode() {
        window.location.href = "/registerCode";
    }


    function isIE() { //ie?
        if (!!window.ActiveXObject || "ActiveXObject" in window)
            return true;
        else
            return false;
    }


    // 搜索响应函数
    $("#find").keydown(function (event) {
        if (event.which == "13") {
            findUser();
        }
    });

    $("#newDirText").keydown(function (event) {
        if (event.which == "13") {
            $("#confirmDirBtn").click();
        }
    });

    // 用户搜索核心函数
    function findUser() {
        var cell_start = 1;
        var input = $("#find").val();
        //alert(input);
        if (input == "") {
            alert("请输入搜索");
        } else {
            $.ajax({
                type: "GET",
                url: "/getUserList",
                data: {searchWord: input},
                dataType: "json",
                success: function (data) {
                    // 将现在的表内容存储并清空
                    var oTabNow = document.getElementById('table_filelist');
                    var nowTabRowLen = oTabNow.rows.length;
                    for (var i = 1; i < nowTabRowLen; i++) {
                        var rowObj = oTabNow.rows[1];
                        if (isIE()) {
                            rowObj.parentNode.removeChild(rowObj);
                        } else {
                            rowObj.remove();
                        }
                    }
                    var userIds = new Array();
                    for (var i = 0; i < data.total; i++) {
                        var rowObj = oTabNow.insertRow(oTabNow.rows.length); // 添加一行
                        rowObj.insertCell(0).innerHTML = '<img src="images/timg.jpg" style="height: 20px;">';
                        rowObj.insertCell(cell_start).innerHTML = '<a>' + data.records[i].username + '</a>';
                        rowObj.insertCell(cell_start + 1).innerHTML = '<a>' + data.records[i].email + '</a>';
                        rowObj.insertCell(cell_start + 2).innerHTML = '<a>' + data.records[i].phone + '</a>';
                        rowObj.insertCell(cell_start + 3).innerHTML = '<a>' + $.myTime.UnixToDate(data.records[i].createDate,true) + '</a>';
                        rowObj.insertCell(cell_start + 4).innerHTML = '<a>密码重置</a>';
                        rowObj.insertCell(cell_start + 5).innerHTML = '<a>设为管理员</a>';
                        rowObj.insertCell(cell_start + 6).innerHTML = '<a>删除</a>';
                        userIds.push(data.records[i].id);
                        // 密码重置响应事件
                        rowObj.getElementsByTagName('a')[4].onclick = function () {
                            var tempIdx = this.parentNode.parentNode.rowIndex;
                            $.ajax({
                                type: "PUT",
                                url: "/alterPassword",
                                data: {id: userIds[tempIdx-1]},
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == "200") {
                                        alert("密码重置成功，新密码已发送至用户邮箱！")
                                    }else {
                                        alert(data.msg);
                                    }
                                }
                            });
                        }
                        // 权限修改响应事件
                        rowObj.getElementsByTagName('a')[5].onclick = function () {
                            var tempIdx = this.parentNode.parentNode.rowIndex;
                            $.ajax({
                                type: "PUT",
                                url: "/updateAuth",
                                data: {id: userIds[tempIdx-1]},
                                dataType: "json",
                                success: function (data) {
                                    if (data.code == "200") {
                                        refreshUserList();
                                    }else {
                                        alert(data.msg);
                                    }
                                }
                            });
                        }
                        // 删除操作响应事件
                        rowObj.getElementsByTagName('a')[6].onclick = function () {
                            var tempIdx = this.parentNode.parentNode.rowIndex;
                            if(window.confirm("确定删除吗？")){
                                $.ajax({
                                    type: "DELETE",
                                    url: "/deleteUser",
                                    data: {id: userIds[tempIdx-1]},
                                    dataType: "json",
                                    success: function (data) {
                                        if (data.code == "200"){
                                            refreshUserList();
                                        }else {
                                            alert(data.msg);
                                        }
                                    }
                                });
                            }
                        }
                    }
                }
            });
        }
    }


</script>
</body>

</html>
